package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.FavoriteQuery;
import cn.org.eshow.bean.query.ProductItemQuery;
import cn.org.eshow.bean.query.ProductQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Favorite;
import cn.org.eshow.model.Product;
import cn.org.eshow.model.ProductItem;
import cn.org.eshow.model.User;
import cn.org.eshow.service.FavoriteManager;
import cn.org.eshow.service.ProductItemManager;
import cn.org.eshow.service.ProductManager;
import cn.org.eshow.webapp.action.response.ProductItemResponse;
import cn.org.eshow.webapp.action.response.ProductResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品API接口
 */
@AllowedMethods({"search", "view"})
public class ProductAction extends ApiBaseAction<Product> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductManager productManager;
    @Autowired
    private ProductItemManager productItemManager;
    @Autowired
    private FavoriteManager favoriteManager;

    private List<Product> products = new ArrayList<Product>();
    private Product product = new Product();
    private ProductQuery query = new ProductQuery(Boolean.TRUE);

    /**
     * 搜索商品列表
     */
    public void search() {
        Page<Product> page = productManager.search(query);
        products = page.getDataList();
        if (products.isEmpty()) {
            RenderUtil.failure("暂无数据");
            return;
        }
        List<ProductResponse> responses = new ArrayList<ProductResponse>();
        for (Product obj : products) {
            ProductResponse response = new ProductResponse(obj);
            responses.add(response);
        }
        RenderUtil.page("获取成功", "products", page, responses);
    }

    /**
     * 获取商品信息,传入accessToken,将返回是否已经收藏该商品
     */
    public void view() {
        product = productManager.get(id);
        if (product == null) {
            RenderUtil.failure("非法参数");
            return;
        }
        ProductResponse response = new ProductResponse(product);

        User user = accessTokenManager.isValid(accessToken);
        if (user != null) {
            Favorite old= favoriteManager.check(new FavoriteQuery(id,user.getId(),true));
            if(old!=null){
                response.setIfFavorite(true);
            }
        }
        //详细商品列表
        List<ProductItemResponse> productItemResponses=new ArrayList<ProductItemResponse>();
        List<ProductItem> productItemList=productItemManager.list(new ProductItemQuery(1,id,true));
        for (ProductItem obj : productItemList) {
            ProductItemResponse itemResponse = new ProductItemResponse(obj);
            productItemResponses.add(itemResponse);
        }
        response.setProductItems(productItemResponses);
        RenderUtil.success("获取成功", "product", response);
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public ProductQuery getQuery() {
        return query;
    }

    public void setQuery(ProductQuery query) {
        this.query = query;
    }

}